﻿/***************************************************************
* File    : GUIConfig.cs
* Author  : Matthew Clifton
*
* Description:
*   Form that gives user options related to sensory displays
*
* History :
***************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;

namespace RLGDashboard
{
    public partial class GUIConfiguration : Form
    {
        XMLController xml = new XMLController();
        RangeContainer ranges;

        public GUIConfiguration()
        {
            this.ranges = xml.retrieveFromXml();
            InitializeComponent();
            initializeObjects();
            loadConfiguration();
            initializeLists();                        
        }

        private void initializeLists()
        {
            initializeEGT1();
            initializeEGT2();
            initializeEGT3();
            initializeEGT4();

            initializeRPM1();
            initializeRPM2();
            initializeRPM3();

            initializePSI1();
            initializePSI2();
            initializePSI3();

            initializeAFR();

            initializeVoltage();
        }

        private void initializeEGT1()
        {
            egt1RangeList.Items.Clear();
            foreach (Range range in ranges.egt1Container)
            {
                egt1RangeList.Items.Add(range.color);
            }
        }

        private void initializeEGT2()
        {
            egt2RangeList.Items.Clear();
            foreach (Range range in ranges.egt2Container)
            {
                egt2RangeList.Items.Add(range.color);
            }
        }

        private void initializeEGT3()
        {
            egt3RangeList.Items.Clear();
            foreach (Range range in ranges.egt3Container)
            {
                egt3RangeList.Items.Add(range.color);
            }
        }

        private void initializeEGT4()
        {
            egt4RangeList.Items.Clear();
            foreach (Range range in ranges.egt4Container)
            {
                egt4RangeList.Items.Add(range.color);
            }
        }

        private void initializeRPM1()
        {
            rpm1RangeList.Items.Clear();
            foreach (Range range in ranges.rpm1Container)
            {
                rpm1RangeList.Items.Add(range.color);
            }
        }

        private void initializeRPM2()
        {
            rpm2RangeList.Items.Clear();
            foreach (Range range in ranges.rpm2Container)
            {
                rpm2RangeList.Items.Add(range.color);
            }
        }

        private void initializeRPM3()
        {
            rpm3RangeList.Items.Clear();
            foreach (Range range in ranges.rpm3Container)
            {
                rpm3RangeList.Items.Add(range.color);
            }
        }

        private void initializePSI1()
        {
            psi1RangeList.Items.Clear();
            foreach (Range range in ranges.psi1Container)
            {
                psi1RangeList.Items.Add(range.color);
            }
        }

        private void initializePSI2()
        {
            psi2RangeList.Items.Clear();
            foreach (Range range in ranges.psi2Container)
            {
                psi2RangeList.Items.Add(range.color);
            }
        }

        private void initializePSI3()
        {
            psi3RangeList.Items.Clear();
            foreach (Range range in ranges.psi3Container)
            {
                psi3RangeList.Items.Add(range.color);
            }
        }

        private void initializeAFR()
        {
            afrRangeList.Items.Clear();
            foreach (Range range in ranges.afrContainer)
            {
                afrRangeList.Items.Add(range.color);
            }
        }

        private void initializeVoltage()
        {
            voltageRangeList.Items.Clear();
            foreach (Range range in ranges.voltageContainer)
            {
                voltageRangeList.Items.Add(range.color);
            }
        }

        private void initializeObjects()
        {
            intializeComboBox(cmboEgt1LineColor);
            intializeComboBox(cmboEgt2LineColor);
            intializeComboBox(cmboEgt3LineColor);
            intializeComboBox(cmboEgt4LineColor);

            intializeComboBox(cmboRPM1LineColor);
            intializeComboBox(cmboRPM2LineColor);
            intializeComboBox(cmboRPM3LineColor);

            intializeComboBox(cmboPSI1LineColor);
            intializeComboBox(cmboPSI2LineColor);
            intializeComboBox(cmboPSI3LineColor);

            intializeComboBox(cmboAFRLineColor);

            intializeComboBox(cmboVoltageLineColor);
        }

        private void intializeComboBox(ComboBox combobox)
        {
            combobox.DropDownStyle = ComboBoxStyle.DropDownList;
            Type colorType = typeof(System.Drawing.Color);
            // We take only static property to avoid properties like Name, IsSystemColor ...
            PropertyInfo[] propInfos = colorType.GetProperties(BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.Public);
            foreach (PropertyInfo propInfo in propInfos)
            {
                combobox.Items.Add(propInfo.Name);
            }
        }

        private string getGraphType(Control container)
        {
            foreach (var control in container.Controls)
            {
                RadioButton radio = control as RadioButton;

                if (radio != null && radio.Checked)
                {
                    return radio.Text;
                }
            }

            return null;
        }

        private void setGraphType(Control container, string currentGraphType)
        {
            foreach (var control in container.Controls)
            {
                RadioButton radio = control as RadioButton;
                System.Console.WriteLine(currentGraphType);

                if (radio.Text == currentGraphType)
                {
                    radio.Checked = true;
                }
            }
        }

        private void loadConfiguration()
        {
            //EGT 1 Values
            EGT1Name.Text = Properties.Settings.Default.egt1Name;
            cmboEgt1LineColor.SelectedIndex = cmboEgt1LineColor.FindStringExact(Properties.Settings.Default.egtLine1Color);
            setGraphType(egt1Group, Properties.Settings.Default.egt1Graph);
            egt1Min.Value = Properties.Settings.Default.egt1Min;
            egt1Max.Value = Properties.Settings.Default.egt1Max;

            //EGT 2 Values
            EGT2Name.Text = Properties.Settings.Default.egt2Name;
            cmboEgt2LineColor.SelectedIndex = cmboEgt2LineColor.FindStringExact(Properties.Settings.Default.egtLine2Color);
            setGraphType(egt2Group, Properties.Settings.Default.egt2Graph);
            egt2Min.Text = Properties.Settings.Default.egt2Min.ToString();
            egt2Max.Text = Properties.Settings.Default.egt2Max.ToString();
            //EGT 3 Values
            EGT3Name.Text = Properties.Settings.Default.egt3Name;
            cmboEgt3LineColor.SelectedIndex = cmboEgt3LineColor.FindStringExact(Properties.Settings.Default.egtLine3Color);
            setGraphType(egt3Group, Properties.Settings.Default.egt3Graph);
            egt3Min.Text = Properties.Settings.Default.egt3Min.ToString();
            egt3Max.Text = Properties.Settings.Default.egt3Max.ToString();
            //EGT 4 Values
            EGT4Name.Text = Properties.Settings.Default.egt4Name;
            cmboEgt4LineColor.SelectedIndex = cmboEgt4LineColor.FindStringExact(Properties.Settings.Default.egtLine4Color);
            setGraphType(egt4Group, Properties.Settings.Default.egt4Graph);
            egt4Min.Text = Properties.Settings.Default.egt4Min.ToString();
            egt4Max.Text = Properties.Settings.Default.egt4Max.ToString();

            //RPM 1 Values
            RPM1Name.Text = Properties.Settings.Default.rpm1Name;
            cmboRPM1LineColor.SelectedIndex = cmboRPM1LineColor.FindStringExact(Properties.Settings.Default.rpmLine1Color);
            setGraphType(rpm1Group, Properties.Settings.Default.rpm1Graph);
            rpm1Min.Text = Properties.Settings.Default.rpm1Min.ToString();
            rpm1Max.Text = Properties.Settings.Default.rpm1Max.ToString();
            //RPM 2 Values
            RPM2Name.Text = Properties.Settings.Default.rpm2Name;
            cmboRPM2LineColor.SelectedIndex = cmboRPM2LineColor.FindStringExact(Properties.Settings.Default.rpmLine2Color);
            setGraphType(rpm2Group, Properties.Settings.Default.rpm2Graph);
            rpm2Min.Text = Properties.Settings.Default.rpm2Min.ToString();
            rpm2Max.Text = Properties.Settings.Default.rpm2Max.ToString();
            //RPM 3 Values
            RPM3Name.Text = Properties.Settings.Default.rpm3Name;
            cmboRPM3LineColor.SelectedIndex = cmboRPM3LineColor.FindStringExact(Properties.Settings.Default.rpmLine3Color);
            setGraphType(rpm3Group, Properties.Settings.Default.rpm3Graph);
            rpm3Min.Text = Properties.Settings.Default.rpm3Min.ToString();
            rpm3Max.Text = Properties.Settings.Default.rpm3Max.ToString();

            //PSI 1 Values
            PSI1Name.Text = Properties.Settings.Default.psi1Name;
            cmboPSI1LineColor.SelectedIndex = cmboPSI1LineColor.FindStringExact(Properties.Settings.Default.psiLine1Color);
            setGraphType(psi1Group, Properties.Settings.Default.psi1Graph);
            psi1Min.Text = Properties.Settings.Default.psi1Min.ToString();
            psi1Max.Text = Properties.Settings.Default.psi1Max.ToString();
            //PSI 2 Values
            PSI2Name.Text = Properties.Settings.Default.psi2Name;
            cmboPSI2LineColor.SelectedIndex = cmboPSI2LineColor.FindStringExact(Properties.Settings.Default.psiLine2Color);
            setGraphType(psi2Group, Properties.Settings.Default.psi2Graph);
            psi2Min.Text = Properties.Settings.Default.psi2Min.ToString();
            psi2Max.Text = Properties.Settings.Default.psi2Max.ToString();
            //PSI 3 Values
            PSI3Name.Text = Properties.Settings.Default.psi3Name;
            cmboPSI3LineColor.SelectedIndex = cmboPSI3LineColor.FindStringExact(Properties.Settings.Default.psiLine3Color);
            setGraphType(psi3Group, Properties.Settings.Default.psi3Graph);
            psi3Min.Text = Properties.Settings.Default.psi3Min.ToString();
            psi3Max.Text = Properties.Settings.Default.psi3Max.ToString();

            //AFR Values
            AFRName.Text = Properties.Settings.Default.afrName;
            cmboAFRLineColor.SelectedIndex = cmboAFRLineColor.FindStringExact(Properties.Settings.Default.afrLineColor);
            setGraphType(afrGroup, Properties.Settings.Default.afrGraph);
            afrMin.Text = Properties.Settings.Default.afrMin.ToString();
            afrMax.Text = Properties.Settings.Default.afrMax.ToString();

            //Voltage Values
            voltageName.Text = Properties.Settings.Default.voltageName;
            cmboVoltageLineColor.SelectedIndex = cmboVoltageLineColor.FindStringExact(Properties.Settings.Default.voltageLineColor);
            setGraphType(voltageGroup, Properties.Settings.Default.voltageGraph);
            voltageMin.Text = Properties.Settings.Default.voltageMin.ToString("G");
            voltageMax.Text = Properties.Settings.Default.voltageMax.ToString();
        }
        private void saveConfiguration()
        {
            //EGT 1 Values
            Properties.Settings.Default.egtLine1Color = cmboEgt1LineColor.Text;
            Properties.Settings.Default.egt1Name = EGT1Name.Text;
            Properties.Settings.Default.egt1Graph = getGraphType(egt1Group);
            Properties.Settings.Default.egt1Min = (int) egt1Min.Value;
            Properties.Settings.Default.egt1Max = (int) egt1Max.Value;
            //EGT 2 Values
            Properties.Settings.Default.egtLine2Color = cmboEgt2LineColor.Text;
            Properties.Settings.Default.egt2Name = EGT2Name.Text;
            Properties.Settings.Default.egt2Graph = getGraphType(egt2Group);
            Properties.Settings.Default.egt2Min = (int)egt2Min.Value;
            Properties.Settings.Default.egt2Max = (int)egt2Max.Value;
            //EGT 3 Values
            Properties.Settings.Default.egtLine3Color = cmboEgt3LineColor.Text;
            Properties.Settings.Default.egt3Name = EGT3Name.Text;
            Properties.Settings.Default.egt3Graph = getGraphType(egt3Group);
            Properties.Settings.Default.egt3Min = (int)egt3Min.Value;
            Properties.Settings.Default.egt3Max = (int)egt3Max.Value;
            //EGT 4 Values
            Properties.Settings.Default.egtLine4Color = cmboEgt4LineColor.Text;
            Properties.Settings.Default.egt4Name = EGT4Name.Text;
            Properties.Settings.Default.egt4Graph = getGraphType(egt4Group);
            Properties.Settings.Default.egt4Min = (int)egt4Min.Value;
            Properties.Settings.Default.egt4Max = (int)egt4Max.Value;

            //RPM 1 Values
            Properties.Settings.Default.rpmLine1Color = cmboRPM1LineColor.Text;
            Properties.Settings.Default.rpm1Name = RPM1Name.Text;
            Properties.Settings.Default.rpm1Graph = getGraphType(rpm1Group);
            Properties.Settings.Default.rpm1Min = (int)rpm1Min.Value;
            Properties.Settings.Default.rpm1Max = (int)rpm1Max.Value;
            //RPM 2 Values
            Properties.Settings.Default.rpmLine2Color = cmboRPM2LineColor.Text;
            Properties.Settings.Default.rpm2Name = RPM2Name.Text;
            Properties.Settings.Default.rpm2Graph = getGraphType(rpm2Group);
            Properties.Settings.Default.rpm2Min = (int)rpm2Min.Value;
            Properties.Settings.Default.rpm2Max = (int)rpm2Max.Value;
            //RPM 3 Values
            Properties.Settings.Default.rpmLine3Color = cmboRPM3LineColor.Text;
            Properties.Settings.Default.rpm3Name = RPM3Name.Text;
            Properties.Settings.Default.rpm3Graph = getGraphType(rpm3Group);
            Properties.Settings.Default.rpm3Min = (int)rpm3Min.Value;
            Properties.Settings.Default.rpm3Max = (int)rpm3Max.Value;

            //PSI 1 Values
            Properties.Settings.Default.psiLine1Color = cmboPSI1LineColor.Text;
            Properties.Settings.Default.psi1Name = PSI1Name.Text;
            Properties.Settings.Default.psi1Graph = getGraphType(psi1Group);
            Properties.Settings.Default.psi1Min = (int)psi1Min.Value;
            Properties.Settings.Default.psi1Max = (int)psi1Max.Value;
            //PSI 2 Values
            Properties.Settings.Default.psiLine2Color = cmboPSI2LineColor.Text;
            Properties.Settings.Default.psi2Name = PSI2Name.Text;
            Properties.Settings.Default.psi2Graph = getGraphType(psi2Group);
            Properties.Settings.Default.psi2Min = (int)psi2Min.Value;
            Properties.Settings.Default.psi2Max = (int)psi2Max.Value;
            //PSI 3 Values
            Properties.Settings.Default.psiLine3Color = cmboPSI3LineColor.Text;
            Properties.Settings.Default.psi3Name = PSI3Name.Text;
            Properties.Settings.Default.psi3Graph = getGraphType(psi3Group);
            Properties.Settings.Default.psi3Min = (int)psi3Min.Value;
            Properties.Settings.Default.psi3Max = (int)psi3Max.Value;

            //Voltage Values
            Properties.Settings.Default.voltageLineColor = cmboVoltageLineColor.Text;
            Properties.Settings.Default.voltageName = voltageName.Text;
            Properties.Settings.Default.afrGraph = getGraphType(group17);
            Properties.Settings.Default.voltageMin = (int)voltageMin.Value;
            Properties.Settings.Default.voltageMax = (int)voltageMax.Value;

            //AFR Values
            Properties.Settings.Default.afrLineColor = cmboAFRLineColor.Text;
            Properties.Settings.Default.afrName = AFRName.Text;
            Properties.Settings.Default.voltageGraph = getGraphType(voltageGroup);
            Properties.Settings.Default.afrMin = (int)afrMin.Value;
            Properties.Settings.Default.afrMax = (int)afrMax.Value;

            Properties.Settings.Default.Save();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void applyButton_Click(object sender, EventArgs e)
        {
            saveConfiguration();
            xml.serializeToXML(ranges);
            this.Close();
        }

        protected void OnDrawItem(Object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();

            string s = (string) cmboEgt2LineColor.Items[e.Index];

            using (Brush b = new SolidBrush(Color.FromName(s)))
            {
                e.Graphics.DrawRectangle(Pens.Black, 2, e.Bounds.Top + 1, 20, 12);
                e.Graphics.FillRectangle(b, 3, e.Bounds.Top + 2, 19, 11);
                e.Graphics.DrawString(s, new Font("Arial", 9, FontStyle.Regular), Brushes.Black, 23, e.Bounds.Top + 1); 
            }

            e.DrawFocusRectangle();
        }

        private void egt1AddRangeButton_Click(object sender, EventArgs e)
        {
            RangeForm addRangeForm = new RangeForm(Properties.Settings.Default.egt1Min, Properties.Settings.Default.egt1Max);
            DialogResult result = addRangeForm.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.ranges.addEGT1Range(addRangeForm.min, addRangeForm.max, addRangeForm.color);
                initializeEGT1();
            }
        }

        private void egt1RemoveRangeButton_Click(object sender, EventArgs e)
        {
            if (egt1RangeList.SelectedIndex == -1)
            {
                MessageBox.Show("No range selected.");
            }

            else
            {
                this.ranges.removeEGT1Range(egt1RangeList.SelectedIndex);
                initializeEGT1();
            }
        }

        private void egt2AddRangeButton_Click(object sender, EventArgs e)
        {
            RangeForm addRangeForm = new RangeForm(Properties.Settings.Default.egt2Min, Properties.Settings.Default.egt2Max);
            DialogResult result = addRangeForm.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.ranges.addEGT2Range(addRangeForm.min, addRangeForm.max, addRangeForm.color);
                initializeEGT2();
            }
        }

        private void egt2RemoveRangeButton_Click(object sender, EventArgs e)
        {
            if (egt2RangeList.SelectedIndex == -1)
            {
                MessageBox.Show("No range selected.");
            }

            else
            {
                this.ranges.removeEGT2Range(egt2RangeList.SelectedIndex);
                initializeEGT2();
            }
        }

        private void egt3AddRangeButton_Click(object sender, EventArgs e)
        {
            RangeForm addRangeForm = new RangeForm(Properties.Settings.Default.egt3Min, Properties.Settings.Default.egt3Max);
            DialogResult result = addRangeForm.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.ranges.addEGT3Range(addRangeForm.min, addRangeForm.max, addRangeForm.color);
                initializeEGT3();
            }
        }

        private void Egt3RemoveRangeButton_Click(object sender, EventArgs e)
        {
            if (egt3RangeList.SelectedIndex == -1)
            {
                MessageBox.Show("No range selected.");
            }

            else
            {
                this.ranges.removeEGT3Range(egt3RangeList.SelectedIndex);
                initializeEGT3();
            }
        }

        private void egt4AddRangeButton_Click(object sender, EventArgs e)
        {
            RangeForm addRangeForm = new RangeForm(Properties.Settings.Default.egt4Min, Properties.Settings.Default.egt4Max);
            DialogResult result = addRangeForm.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.ranges.addEGT4Range(addRangeForm.min, addRangeForm.max, addRangeForm.color);
                initializeEGT4();
            }
        }

        private void egt4RemoveRangeButton_Click(object sender, EventArgs e)
        {
            if (egt4RangeList.SelectedIndex == -1)
            {
                MessageBox.Show("No range selected.");
            }

            else
            {
                this.ranges.removeEGT4Range(egt4RangeList.SelectedIndex);
                initializeEGT4();
            }
        }

        private void rpm1AddRangeButton_Click(object sender, EventArgs e)
        {
            RangeForm addRangeForm = new RangeForm(Properties.Settings.Default.rpm1Min, Properties.Settings.Default.rpm1Max);
            DialogResult result = addRangeForm.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.ranges.addRPM1Range(addRangeForm.min, addRangeForm.max, addRangeForm.color);
                initializeRPM1();
            }
        }

        private void rpm1RemoveRangeButton_Click(object sender, EventArgs e)
        {
            if (rpm1RangeList.SelectedIndex == -1)
            {
                MessageBox.Show("No range selected.");
            }

            else
            {
                this.ranges.removeRPM1Range(rpm1RangeList.SelectedIndex);
                initializeRPM1();
            }
        }

        private void rpm2AddRangeButton_Click(object sender, EventArgs e)
        {
            RangeForm addRangeForm = new RangeForm(Properties.Settings.Default.rpm2Min, Properties.Settings.Default.rpm2Max);
            DialogResult result = addRangeForm.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.ranges.addRPM2Range(addRangeForm.min, addRangeForm.max, addRangeForm.color);
                initializeRPM2();
            }
        }

        private void rpm2RemoveRangeButton_Click(object sender, EventArgs e)
        {
            if (rpm2RangeList.SelectedIndex == -1)
            {
                MessageBox.Show("No range selected.");
            }

            else
            {
                this.ranges.removeRPM2Range(rpm2RangeList.SelectedIndex);
                initializeRPM2();
            }
        }

        private void rpm3AddRangeButton_Click(object sender, EventArgs e)
        {
            RangeForm addRangeForm = new RangeForm(Properties.Settings.Default.rpm3Min, Properties.Settings.Default.rpm3Max);
            DialogResult result = addRangeForm.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.ranges.addRPM3Range(addRangeForm.min, addRangeForm.max, addRangeForm.color);
                initializeRPM3();
            }
        }

        private void rpm3RemoveRangeButton_Click(object sender, EventArgs e)
        {
            if (rpm3RangeList.SelectedIndex == -1)
            {
                MessageBox.Show("No range selected.");
            }

            else
            {
                this.ranges.removeRPM3Range(rpm3RangeList.SelectedIndex);
                initializeRPM3();
            }
        }

        private void psi1AddRangeButton_Click(object sender, EventArgs e)
        {
            RangeForm addRangeForm = new RangeForm(Properties.Settings.Default.psi1Min, Properties.Settings.Default.psi1Max);
            DialogResult result = addRangeForm.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.ranges.addPSI1Range(addRangeForm.min, addRangeForm.max, addRangeForm.color);
                initializePSI1();
            }
        }

        private void psi1RemoveRangeButton_Click(object sender, EventArgs e)
        {
            if (psi1RangeList.SelectedIndex == -1)
            {
                MessageBox.Show("No range selected.");
            }

            else
            {
                this.ranges.removePSI1Range(psi1RangeList.SelectedIndex);
                initializePSI1();
            }
        }

        private void psi2AddRangeButton_Click(object sender, EventArgs e)
        {
            RangeForm addRangeForm = new RangeForm(Properties.Settings.Default.psi2Min, Properties.Settings.Default.psi2Max);
            DialogResult result = addRangeForm.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.ranges.addPSI2Range(addRangeForm.min, addRangeForm.max, addRangeForm.color);
                initializePSI2();
            }
        }

        private void psi2RemoveRangeButton_Click(object sender, EventArgs e)
        {
            if (psi2RangeList.SelectedIndex == -1)
            {
                MessageBox.Show("No range selected.");
            }

            else
            {
                this.ranges.removePSI2Range(psi2RangeList.SelectedIndex);
                initializePSI2();
            }
        }

        private void psi3AddRangeButton_Click(object sender, EventArgs e)
        {
            RangeForm addRangeForm = new RangeForm(Properties.Settings.Default.psi3Min, Properties.Settings.Default.psi3Max);
            DialogResult result = addRangeForm.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.ranges.addPSI3Range(addRangeForm.min, addRangeForm.max, addRangeForm.color);
                initializePSI3();
            }
        }

        private void psi3RemoveRangeButton_Click(object sender, EventArgs e)
        {
            if (psi3RangeList.SelectedIndex == -1)
            {
                MessageBox.Show("No range selected.");
            }

            else
            {
                this.ranges.removePSI3Range(psi3RangeList.SelectedIndex);
                initializePSI3();
            }
        }

        private void afrAddRangeButton_Click(object sender, EventArgs e)
        {
            RangeForm addRangeForm = new RangeForm(Properties.Settings.Default.afrMin, Properties.Settings.Default.afrMax);
            DialogResult result = addRangeForm.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.ranges.addAFRRange(addRangeForm.min, addRangeForm.max, addRangeForm.color);
                initializeAFR();
            }
        }

        private void afrRemoveRangeButton_Click(object sender, EventArgs e)
        {
            if (afrRangeList.SelectedIndex == -1)
            {
                MessageBox.Show("No range selected.");
            }

            else
            {
                this.ranges.removeAFRRange(afrRangeList.SelectedIndex);
                initializeAFR();
            }
        }

        private void voltageAddRangeButton_Click(object sender, EventArgs e)
        {
            RangeForm addRangeForm = new RangeForm(Properties.Settings.Default.voltageMin, Properties.Settings.Default.voltageMax);
            DialogResult result = addRangeForm.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.ranges.addVoltageRange(addRangeForm.min, addRangeForm.max, addRangeForm.color);
                initializeVoltage();
            }
        }

        private void voltageRemoveRangeButton_Click(object sender, EventArgs e)
        {
            if (voltageRangeList.SelectedIndex == -1)
            {
                MessageBox.Show("No range selected.");
            }

            else
            {
                this.ranges.removeVoltageRange(voltageRangeList.SelectedIndex);
                initializeVoltage();
            }
        }

    }
}
